package practice.library.webmagic.processor.qyer

import java.net.URLEncoder

/**
  * Created by lovepocky(lovepocky@foxmail.com) on 2016/6/17.
  */
object QyerCommon {
	
	object regexPattern {
		def place = "http://place.qyer.com/\\w+/"

		def subPlace = "http://place.qyer.com/poi/\\w+/"
		
		def allSubPlacePages(city: String) = s"http://place.qyer.com/${city }/alltravel([?]page=\\d+|/)"
	}
	
	object api {
		def search(keyword: String) = s"http://place.qyer.com/ajax.php?action=ajaxplacesearch&kw=${URLEncoder.encode(keyword, "UTF-8") }"
		
		def city(city: String) = s"http://place.qyer.com/${city }/"
		
		def allSubPlace(city: String) = s"http://place.qyer.com/${city }/alltravel"
		
		def allSubPlacePages(city: String, page: Int) = s"http://place.qyer.com/${city }/alltravel/?page=${page }"
		
	}
	
	object transform {
		def cityUrl2City(url: String): Option[String] = if (url.matches(api.city("\\w+"))) Option(url.substring(22, url.length - 1)) else None
		
		def findMaxPage(allSubPlacePagesSeq: Seq[String]): Int = {
			allSubPlacePagesSeq.map(it => it.substring(it.indexOf("?page=") + 6, it.length).toInt).max
		}
	}
	
}
